﻿Option Explicit On
Option Strict On

Imports System.Data

Partial Class private_secure_members_manage_MyContent
    Inherits System.Web.UI.Page

    Dim u As MembershipUser
    Dim personId As Int32
    Dim delid As Integer
    Dim entityid As Integer
    'Protected Sub GridView3_PreRender(sender As Object, e As EventArgs)
    '    If GridView3.Rows.Count > 0 Then
    '        GridView3.UseAccessibleHeader = True
    '        GridView3.HeaderRow.TableSection = TableRowSection.TableHeader
    '    End If
    'End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        u = Membership.GetUser(HttpContext.Current.User.Identity.Name)
        Dim getpersonid As New GetPersonIdFromUserId()

        If Not Page.IsPostBack Then
            If Not Session("EntityID") Is Nothing Then
                ddlEntity.SelectedValue = CStr(Session("EntityID"))
            End If
        End If

        Try
            personId = getpersonid.GetPersonId(CType(u.ProviderUserKey, Guid))
            Dim entityidTA As New LogginsLogDSTableAdapters.usp_GetEntityListFull_ByPersonIdTableAdapter()
            Dim entityidDT As LogginsLogDS.usp_GetEntityListFull_ByPersonIdDataTable
            entityidDT = entityidTA.GetData(personId)

            Me.GridView1.DataBind()
        Catch ex As Exception
            Throw ex
        End Try
        If GridView1.SelectedValue Is Nothing Then
            'Panel1.Visible = False
            Panel2.Visible = False
            'Panel4.Visible = False
        Else
            'Panel1.Visible = True
            Panel2.Visible = True
            'Panel4.Visible = True

        End If

    End Sub

    Protected Sub SqlEntity_ds_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlEntity_ds.Selecting
        e.Command.Parameters("@PersonId").Value = CType(personId, Integer)
    End Sub

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        'Now call this stored procedure to do the work
        'EXEC dbo.usp_CopySystemContentToUserContentByGroupId @SystemMessageGroupId INT,   @UserMessageGroupId INT
        '@SystemMessageGroupId is the system group selected
        '@UserMessageGroupId is the usergroupnames (already selected or created) to which to copy the items to
        If Not CDbl(ddlSystemMessageGroup.SelectedValue) = 0 Then


            If Not GridView1.SelectedValue Is Nothing Then
                Try
                    Dim copysystemcontentTA As New LogginsLogDSTableAdapters.SPTableAdapter()
                    copysystemcontentTA.usp_CopySystemContentToUserContentByGroupId(CType(ddlSystemMessageGroup.SelectedValue, Integer?), CType(GridView1.SelectedValue, Integer?))
                Catch ex As Exception
                    Throw ex
                End Try
                Label6.Text = ""
            Else
                Label6.Text = "Please select item from saved content"
            End If
            UpdatePanel2.UpdateMode = UpdatePanelUpdateMode.Conditional
            UpdatePanel2.Update()
            GridView1.DataBind()
            GridView2.DataBind()
            GridView3.DataBind()
        End If

    End Sub

    Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        If CDbl(ddlEntity.SelectedValue) = 0 Then

        Else
            btnInsert.Visible = True
            btnUpdate.Visible = False
            InsertPopupExtender.Show()
            lblAction.Text = "Insert new group"

        End If

    End Sub

    Protected Sub btnInsert_Click(sender As Object, e As System.EventArgs) Handles btnInsert.Click
        Try
            Dim newgroupTA As New LogginsLogDSTableAdapters.usp_UserSavedMessageGroupTableAdapter()
            Dim newgroupid = newgroupTA.CustomInsert(CType(ddlEntity.SelectedValue, Integer?), txtGroupName.Text, CType(DropDownList1.SelectedValue, Byte?), DateTime.Now, True)
        Catch ex As Exception
            Throw ex
        End Try

        GridView1.DataBind()
    End Sub


    Protected Sub btnUpdate_Click(sender As Object, e As System.EventArgs) Handles btnUpdate.Click
        Try
            Dim newgroupTA As New LogginsLogDSTableAdapters.usp_UserSavedMessageGroupTableAdapter()
            Dim newgroupDT As LogginsLogDS.usp_UserSavedMessageGroupDataTable
            newgroupDT = newgroupTA.GetData(CType(Label2.Text, Integer?))
            For Each groupRow As LogginsLogDS.usp_UserSavedMessageGroupRow In newgroupDT
                newgroupTA.Update(groupRow.Id, groupRow.EntityId, txtGroupName.Text, CType(DropDownList1.SelectedValue, Byte?), groupRow.DateAdded, True)
            Next
        Catch ex As Exception
            Throw ex
        End Try
        GridView1.DataBind()

    End Sub

    Protected Sub ddlEntity_DataBound(sender As Object, e As System.EventArgs) Handles ddlEntity.DataBound
        GridView1.DataBind()
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        'Panel1.Visible = True
        Panel2.Visible = True
        'Panel4.Visible = True
    End Sub

    Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "CustomEdit" Then
            Dim selectedID As Integer = CInt(e.CommandArgument.ToString())
            Try
                Dim newgroupTA As New LogginsLogDSTableAdapters.usp_UserSavedMessageGroupTableAdapter()
                Dim newgroupDT As LogginsLogDS.usp_UserSavedMessageGroupDataTable
                newgroupDT = newgroupTA.GetData(CType(selectedID, Integer?))
                For Each groupRow As LogginsLogDS.usp_UserSavedMessageGroupRow In newgroupDT
                    txtGroupName.Text = groupRow.GroupName
                    DropDownList1.SelectedValue = CStr(groupRow.MessageTypeId)
                Next
            Catch ex As Exception
                Throw ex
            End Try

            lblAction.Text = "Edit Message Group"
            btnInsert.Visible = False
            btnUpdate.Visible = True
            Label2.Text = CStr(selectedID)
            InsertPopupExtender.Show()
        End If

    End Sub

    Protected Sub Button7_Click(sender As Object, e As System.EventArgs) Handles Button7.Click
        Button5.Visible = False
        Button4.Visible = True
        Label4.Text = ""
        up.Update()
        ModalPopupExtender1.Show()

    End Sub

    Protected Sub Button4_Click(sender As Object, e As System.EventArgs) Handles Button4.Click
        If Not TextBox2.Text = String.Empty Then
            Dim messageTA As New LogginsLogDSTableAdapters.usp_UserSavedMessageGroupItemTableAdapter()
            messageTA.Insert(CType(GridView1.SelectedValue, Integer?), 1, TextBox2.Text, True)
            GridView3.DataBind()
            TextBox2.Text = String.Empty
            'fixseqno()
            UpdatePanel2.UpdateMode = UpdatePanelUpdateMode.Conditional
            UpdatePanel2.Update()
            ModalPopupExtender1.Hide()
        Else
            Label4.Text = "You can not add empty message."

        End If



    End Sub

    Protected Sub Button5_Click(sender As Object, e As System.EventArgs) Handles Button5.Click
        Dim messageTA As New LogginsLogDSTableAdapters.usp_UserSavedMessageGroupItemTableAdapter()
        Dim messageDT As LogginsLogDS.usp_UserSavedMessageGroupItemDataTable
        messageDT = messageTA.GetData(Nothing)
        Dim seqno = (From s In messageDT.AsEnumerable() Where s.Id = Convert.ToInt32(Label4.Text) Select s).FirstOrDefault()

        messageTA.Update(CType(Label4.Text, Integer?), CType(GridView1.SelectedValue, Integer?), seqno.SeqNo, TextBox2.Text, True)
        GridView3.DataBind()
        TextBox2.Text = String.Empty
        fixseqno()
    End Sub

    Protected Sub GridView3_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView3.RowCommand
        If e.CommandName = "CustomEdit" Then
            Dim selectedID As Integer = CInt(e.CommandArgument.ToString())
            Dim messsageTA As New LogginsLogDSTableAdapters.usp_UserSavedMessageGroupItemTableAdapter()
            Dim messageDT As LogginsLogDS.usp_UserSavedMessageGroupItemDataTable
            messageDT = messsageTA.GetData(selectedID)
            For Each messageRow As LogginsLogDS.usp_UserSavedMessageGroupItemRow In messageDT
                Label4.Text = CStr(messageRow.Id)
                TextBox2.Text = messageRow.MessageText
            Next
            Button4.Visible = False
            Button5.Visible = True
            ModalPopupExtender1.Show()
        ElseIf e.CommandName = "CustomDelete" Then
            Dim selectedID As Integer = CInt(e.CommandArgument.ToString())
            Dim messsageTA As New LogginsLogDSTableAdapters.usp_UserSavedMessageGroupItemTableAdapter()
            messsageTA.Delete(selectedID)
            GridView3.DataBind()
        End If
    End Sub

    'Protected Sub GridView1_PreRender(sender As Object, e As System.EventArgs) Handles GridView1.PreRender
    '    If GridView1.Rows.Count > 0 Then
    '        GridView1.UseAccessibleHeader = True
    '        GridView1.HeaderRow.TableSection = TableRowSection.TableHeader
    '    End If

    'End Sub

    'Protected Sub Button8_Click(sender As Object, e As System.EventArgs) Handles Button8.Click
    '    Dim ss As String = ""

    '    For Each row As GridViewRow In GridView3.Rows
    '        'for templated control
    '        'Dim tb As TextBox = TryCast(row.FindControl("yourtextboxid"), TextBox)
    '        'for bound field
    '        ss += row.Cells(2).Text
    '    Next
    '    Response.Write(ss)
    'End Sub
    Sub fixseqno()
        Dim table As New DataTable
        ' Create four typed columns in the DataTable.
        table.Columns.Add("ReqNo", GetType(Integer))
        table.Columns.Add("Message", GetType(String))

        Dim list As New List(Of String)
        Dim total As String = ""
        For Each gvr As GridViewRow In GridView3.Rows
            Dim ReeqNoText As DropDownList = CType(gvr.FindControl("DropDownList2"), DropDownList)
            Dim MessageText As Label = CType(gvr.FindControl("Label1"), Label)
            total += ReeqNoText.Text + ","
            total += MessageText.Text + "</br>"
            table.Rows.Add(ReeqNoText.Text, MessageText.Text)
            list.Add(ReeqNoText.Text)
        Next
        Dim strDetail As String = ""
        Dim duplicates = list.GroupBy(Function(s) s).SelectMany(Function(grp) grp.Skip(1))
        Dim q = From s In table.AsEnumerable() Order By s.Item("ReqNo") Ascending Select s
        Dim count As Integer = duplicates.Count

        If count = 0 Then
            DeleteMessages()
            For Each row As DataRow In q
                'System.Diagnostics.Debug.WriteLine(CStr(row.Item("ReqNo")) + CStr(row.Item("Message")))
                Dim messageTA As New LogginsLogDSTableAdapters.usp_UserSavedMessageGroupItemTableAdapter()
                messageTA.Insert(CType(GridView1.SelectedValue, Integer?), CType(row.Item("ReqNo").ToString(), Byte?), CStr(row.Item("Message")), True)
                'messageTA.Insert(CType(GridView1.SelectedValue, Integer?), 1, CStr(row.Item("Message")), True)
            Next
            Dim fixmessageTA As New LogginsLogDSTableAdapters.SPTableAdapter()
            fixmessageTA.usp_UserGroupMessageItem_FixSeqNo(CType(GridView1.SelectedValue, Integer?))
            Label5.Text = ""

        Else
            Label5.Text = "There are duplicates"
        End If
        'Response.Write(strDetail)
        GridView3.DataBind()
        UpdatePanel2.UpdateMode = UpdatePanelUpdateMode.Conditional
        UpdatePanel2.Update()
    End Sub
    Protected Sub Button8_Click(sender As Object, e As System.EventArgs) Handles Button8.Click
        fixseqno()

    End Sub
    Sub DeleteMessages()
        Dim messageTA As New LogginsLogDSTableAdapters.usp_UserContentMessageGroupItemByIdTableAdapter()
        Dim messageDT As LogginsLogDS.usp_UserContentMessageGroupItemByIdDataTable
        messageDT = messageTA.GetData(CType(GridView1.SelectedValue, Integer?))
        For Each messageRow As LogginsLogDS.usp_UserContentMessageGroupItemByIdRow In messageDT
            Dim messagedelTA As New LogginsLogDSTableAdapters.usp_UserSavedMessageGroupItemTableAdapter()
            messagedelTA.Delete(messageRow.GroupItemId)
        Next
    End Sub

    Protected Sub Button9_Click(sender As Object, e As System.EventArgs) Handles Button9.Click
        Panel5_ModalPopupExtender.Show()
    End Sub

    Protected Sub ddlSystemMessageGroup_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlSystemMessageGroup.SelectedIndexChanged
        If Not CDbl(ddlSystemMessageGroup.SelectedValue) = 0 Then
            GridView2.DataBind()

        End If

    End Sub

    'Protected Sub GridView3_PreRender(sender As Object, e As EventArgs) Handles GridView3.PreRender
    '    fixseqno()
    'End Sub

    Protected Sub GridView3_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView3.RowUpdating
        Dim index As Integer = GridView3.EditIndex
        Dim row As GridViewRow = GridView3.Rows(index)
        Dim message As TextBox = CType(row.FindControl("TextBox2"), TextBox)
        Dim reqno As DropDownList = CType(row.FindControl("DropDownList2"), DropDownList)
        Dim reqid As Label = CType(row.FindControl("Label2"), Label)
        'Response.Write(reqno.Text)
        Dim messageTA As New LogginsLogDSTableAdapters.usp_UserSavedMessageGroupItemTableAdapter()
        Dim messageDT As LogginsLogDS.usp_UserSavedMessageGroupItemDataTable
        messageDT = messageTA.GetData(Nothing)
        Dim seqno = (From s In messageDT.AsEnumerable() Where s.Id = Convert.ToInt32(reqid.Text) Select s).FirstOrDefault()

        messageTA.Update(seqno.Id, CType(GridView1.SelectedValue, Integer?), seqno.SeqNo, message.Text, True)
        GridView3.EditIndex = -1
        GridView3.DataBind()
        UpdatePanel2.UpdateMode = UpdatePanelUpdateMode.Conditional
        UpdatePanel2.Update()

    End Sub

    Protected Sub GridView3_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView3.RowDeleting
        Dim row As GridViewRow = GridView3.Rows(e.RowIndex)
        Dim reqid As Label = CType(row.FindControl("Label3"), Label)
        Dim messageTA As New LogginsLogDSTableAdapters.usp_UserSavedMessageGroupItemTableAdapter()
        messageTA.Delete(CType(reqid.Text, Integer?))
        GridView3.DataBind()

    End Sub

    Protected Sub ddlEntity_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlEntity.SelectedIndexChanged
        If CInt(ddlEntity.SelectedValue) = 0 Then
            Session.Remove("EntityID")
        Else
            Session("EntityID") = ddlEntity.SelectedValue
        End If
    End Sub

    Protected Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        Response.Redirect("~/private/default.aspx")
    End Sub
End Class
